home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / xmppstringprep.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  6KB  |  226 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: xmppstringprep.py,v 1.16 2004/10/07 22:28:04 jajcus Exp $'
  5. __docformat__ = 'restructuredtext en'
  6. import stringprep
  7. import unicodedata
  8. from pyxmpp.exceptions import StringprepError
  9.  
  10. class LookupFunction:
  11.     
  12.     def __init__(self, function):
  13.         self.lookup = function
  14.  
  15.  
  16.  
  17. class LookupTable:
  18.     
  19.     def __init__(self, singles, ranges):
  20.         self.singles = singles
  21.         self.ranges = ranges
  22.  
  23.     
  24.     def lookup(self, c):
  25.         if self.singles.has_key(c):
  26.             return self.singles[c]
  27.         
  28.         c = ord(c)
  29.         for start, end in self.ranges:
  30.             value = None
  31.             if c < start:
  32.                 return None
  33.             
  34.             if c <= end:
  35.                 return value
  36.                 continue
  37.         
  38.  
  39.  
  40. A_1 = LookupFunction(stringprep.in_table_a1)
  41.  
  42. def b1_mapping(uc):
  43.     if stringprep.in_table_b1(uc):
  44.         return u''
  45.     else:
  46.         return None
  47.  
  48. B_1 = LookupFunction(b1_mapping)
  49. B_2 = LookupFunction(stringprep.map_table_b2)
  50. B_3 = LookupFunction(stringprep.map_table_b3)
  51. C_1_1 = LookupFunction(stringprep.in_table_c11)
  52. C_1_2 = LookupFunction(stringprep.in_table_c12)
  53. C_2_1 = LookupFunction(stringprep.in_table_c21)
  54. C_2_2 = LookupFunction(stringprep.in_table_c22)
  55. C_3 = LookupFunction(stringprep.in_table_c3)
  56. C_4 = LookupFunction(stringprep.in_table_c4)
  57. C_5 = LookupFunction(stringprep.in_table_c5)
  58. C_6 = LookupFunction(stringprep.in_table_c6)
  59. C_7 = LookupFunction(stringprep.in_table_c7)
  60. C_8 = LookupFunction(stringprep.in_table_c8)
  61. C_9 = LookupFunction(stringprep.in_table_c9)
  62. D_1 = LookupFunction(stringprep.in_table_d1)
  63. D_2 = LookupFunction(stringprep.in_table_d2)
  64.  
  65. def nfkc(data):
  66.     if type(data) is list:
  67.         data = u''.join(data)
  68.     
  69.     return unicodedata.normalize('NFKC', data)
  70.  
  71.  
  72. class Profile:
  73.     cache_items = []
  74.     
  75.     def __init__(self, unassigned, mapping, normalization, prohibited, bidi = 1):
  76.         self.unassigned = unassigned
  77.         self.mapping = mapping
  78.         self.normalization = normalization
  79.         self.prohibited = prohibited
  80.         self.bidi = bidi
  81.         self.cache = { }
  82.  
  83.     
  84.     def prepare(self, data):
  85.         r = self.cache.get(data)
  86.         if r is not None:
  87.             return r
  88.         
  89.         s = self.map(data)
  90.         if self.normalization:
  91.             s = self.normalization(s)
  92.         
  93.         s = self.prohibit(s)
  94.         s = self.check_unassigned(s)
  95.         if self.bidi:
  96.             s = self.check_bidi(s)
  97.         
  98.         if type(s) is list:
  99.             s = u''.string.join()
  100.         
  101.         if len(self.cache_items) >= stringprep_cache_size:
  102.             remove = self.cache_items[:-stringprep_cache_size / 2]
  103.             for profile, key in remove:
  104.                 
  105.                 try:
  106.                     del profile.cache[key]
  107.                 continue
  108.                 except KeyError:
  109.                     continue
  110.                 
  111.  
  112.             
  113.             self.cache_items[:] = self.cache_items[-stringprep_cache_size / 2:]
  114.         
  115.         self.cache_items.append((self, data))
  116.         self.cache[data] = s
  117.         return s
  118.  
  119.     
  120.     def prepare_query(self, s):
  121.         s = self.map(s)
  122.         if self.normalization:
  123.             s = self.normalization(s)
  124.         
  125.         s = self.prohibit(s)
  126.         if self.bidi:
  127.             s = self.check_bidi(s)
  128.         
  129.         if type(s) is list:
  130.             s = u''.string.join(s)
  131.         
  132.         return s
  133.  
  134.     
  135.     def map(self, s):
  136.         r = []
  137.         for c in s:
  138.             rc = None
  139.             for t in self.mapping:
  140.                 rc = t.lookup(c)
  141.                 if rc is not None:
  142.                     break
  143.                     continue
  144.             
  145.             if rc is not None:
  146.                 r.append(rc)
  147.                 continue
  148.             r.append(c)
  149.         
  150.         return r
  151.  
  152.     
  153.     def prohibit(self, s):
  154.         for c in s:
  155.             for t in self.prohibited:
  156.                 if t.lookup(c):
  157.                     raise StringprepError, 'Prohibited character: %r' % (c,)
  158.                     continue
  159.             
  160.         
  161.         return s
  162.  
  163.     
  164.     def check_unassigned(self, s):
  165.         for c in s:
  166.             for t in self.unassigned:
  167.                 if t.lookup(c):
  168.                     raise StringprepError, 'Unassigned character: %r' % (c,)
  169.                     continue
  170.             
  171.         
  172.         return s
  173.  
  174.     
  175.     def check_bidi(self, s):
  176.         has_l = 0
  177.         has_ral = 0
  178.         for c in s:
  179.             if D_1.lookup(c):
  180.                 has_l = 1
  181.                 continue
  182.             if D_2.lookup(c):
  183.                 has_l = 1
  184.                 continue
  185.         
  186.         if has_l and has_ral:
  187.             raise StringprepError, 'Both RandALCat and LCat characters present'
  188.         
  189.         if has_l:
  190.             if D_1.lookup(s[0]) is None or D_1.lookup(s[-1]) is None:
  191.                 raise StringprepError, 'The first and the last character must be RandALCat'
  192.             
  193.         return s
  194.  
  195.  
  196. nodeprep = Profile(unassigned = (A_1,), mapping = (B_1, B_2), normalization = nfkc, prohibited = (C_1_1, C_1_2, C_2_1, C_2_2, C_3, C_4, C_5, C_6, C_7, C_8, C_9, LookupTable({
  197.     u'"': True,
  198.     u'&': True,
  199.     u"'": True,
  200.     u'/': True,
  201.     u':': True,
  202.     u'<': True,
  203.     u'>': True,
  204.     u'@': True }, ())), bidi = 1)
  205. resourceprep = Profile(unassigned = (A_1,), mapping = (B_1,), normalization = nfkc, prohibited = (C_1_2, C_2_1, C_2_2, C_3, C_4, C_5, C_6, C_7, C_8, C_9), bidi = 1)
  206. stringprep_cache_size = 1000
  207.  
  208. def set_stringprep_cache_size(size):
  209.     global stringprep_cache_size
  210.     stringprep_cache_size = size
  211.     if len(Profile.cache_items) > size:
  212.         remove = Profile.cache_items[:-size]
  213.         for profile, key in remove:
  214.             
  215.             try:
  216.                 del profile.cache[key]
  217.             continue
  218.             except KeyError:
  219.                 continue
  220.             
  221.  
  222.         
  223.         Profile.cache_items = Profile.cache_items[-size:]
  224.     
  225.  
  226.